package com.qfedu.knowledge.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RabbitMQListener
 * 监听队列是否有消息，有的话可以消息
 * @Author ykx
 * @Date 2024/5/6 10:39
 * @Version 1.0
 */
@Component
public class RabbitMQListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //    @RabbitListener(queues = "boot-queue")
//    public void receive(Message msg) {
//        // 消费消息
//        System.out.println("接收到的消息:" + msg.getMessageProperties());
//    }
//    @RabbitListener(queues = "boot-queue")
//    public void getMessage(String msg, Channel channel, Message message) throws IOException {
//        System.out.println("接收到消息：" + msg);
////        int i = 1 / 0;
//        // 手动ack
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }
    @RabbitListener(queues = "boot-queue")
    public void getMessage(String msg, Channel channel, Message message) throws IOException {
        //0. 获取MessageId
        String messageId = (String) message.getMessageProperties().getHeaders().get("spring_returned_message_correlation");
        //1. 设置key到Redis
        if (redisTemplate.opsForValue().setIfAbsent(messageId, "0", 10, TimeUnit.SECONDS)) {
            //2. 消费消息
            System.out.println("接收到消息：" + msg);

            //3. 设置key的value为1
            redisTemplate.opsForValue().set(messageId, "1", 10, TimeUnit.SECONDS);
            //4.  手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            //5. 获取Redis中的value即可 如果是1，手动ack
            if ("1".equalsIgnoreCase(redisTemplate.opsForValue().get(messageId))) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }
        }
    }
}
